<template>
	<div>
		<div class="topTitle">简章公告</div>
		<el-form inline :model="query" label-width="80px">
			<el-form-item label="地区">
				<el-cascader :props="props" v-model="query.key" :options="areaData" placeholder="请选择地区"
					class="width260 margin-right10"></el-cascader>
			</el-form-item>
			<el-form-item label="科目">
				<el-select clearable placeholder="请选择科目" v-model="query.appid" class="width260 margin-right10">
					<el-option v-for="(item,index) in miniData" :key="index" :label="item.name" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="状态">
				<el-select v-model="query.applyStatus" placeholder="请选择状态" class="width260 margin-right10">
					<el-option label="发布" :value="1"></el-option>
					<el-option label="报名中" :value="2"></el-option>
					<el-option label="报名结束" :value="3"></el-option>
					<el-option label="考试结束" :value="4"></el-option>
					<el-option label="公告发布" :value="5"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="标题名称">
				<el-input v-model="query.title" placeholder="请输入关键字" class="width260 margin-right10"></el-input>
			</el-form-item>
			<el-form-item label="发布时间">
				<el-date-picker v-model="dateTime" type="datetimerange" range-separator="至" start-placeholder="开始日期"
					end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" @change="onDateChange">
				</el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" class="margin-left10" @click="handleSearch">搜索</el-button>
				<el-button type="default" icon="el-icon-refresh-right" @click="handleRefresh">重置</el-button>
			</el-form-item>
		</el-form>
		<div class="margin-bottom20">
			<el-button type="primary" @click="handleAdd">新建</el-button>
		</div>
		<el-table :data="tableData" border header-cell-class-name="table-header-style">
			<el-table-column label="序号" type="index" align="center" width="80"></el-table-column>
			<el-table-column label="ID" prop="id" align="center" width="80"></el-table-column>
			<el-table-column label="状态" prop="status" align="center">
				<template slot-scope="{row}">
					<span v-if="row.applyStatus==1">发布</span>
					<span v-else-if="row.applyStatus==2">报名中</span>
					<span v-else-if="row.applyStatus==3">报名结束</span>
					<span v-else-if="row.applyStatus==4">考试结束</span>
					<span v-else-if="row.applyStatus==5">公告发布</span>
				</template>
			</el-table-column>
			<el-table-column label="标题" prop="title" width="400"></el-table-column>
			<el-table-column label="发布类型" prop="stuName" align="center"></el-table-column>
			<el-table-column label="公告年份" prop="year" align="center"></el-table-column>
			<el-table-column label="公告地区" prop="city" align="center">
				<template slot-scope="{row}">
					<el-tag v-if="row.province==0">默认</el-tag>
					<el-tag class="margin-right5"
						v-else-if="row.provinceName&&row.province!=0&&($.isEmpty(row.cityName)||row.city==0)">{{row.provinceName}}</el-tag>
					<el-tag v-if="row.cityName&&row.city!=0">{{row.cityName}}</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="发布时间" prop="publishDate" align="center"></el-table-column>
			<el-table-column label="阅读量" prop="readNum" align="center"></el-table-column>
			<el-table-column label="状态" align="center">
				<template slot-scope="{row}">
					<span v-if="row.status==0">待审核</span>
					<span v-else-if="row.status==1">审核通过</span>
					<span v-else-if="row.status==2">审核失败</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" width="200">
				<template slot-scope="scope">
					<el-button type="text" @click="handleAdd(scope.row,1)">详情</el-button>
					<el-button type="text" @click="handleAdd(scope.row,2)">编辑</el-button>
					<el-popconfirm title="确定删除吗？" @confirm="handleDel(scope.row.id)">
						<el-button type="text" slot="reference">删除</el-button>
					</el-popconfirm>
				</template>
			</el-table-column>
		</el-table>
		<div class="margin-top10">
			<el-pagination background layout="->,total,sizes, prev, pager, next" :current-page="query.pageNumber"
				:page-sizes="[30, 40, 50, 60]" @size-change="handleSizeChange" :page-size="query.pageSize" :total="pageTotal"
				@current-change="handlePageChange"></el-pagination>
		</div>

		<!-- 新建编辑公告 -->
		<el-dialog :title="title" :visible.sync="visible" width="60%">
			<el-form :model="form" label-width="100px" :rules="rules" ref="form">
				<el-form-item label="简章标题" prop="title">
					<el-input v-model="form.title" placeholder="请输入简章标题"></el-input>
				</el-form-item>
				<div class="row-me flex-wrap">
					<el-form-item label="发布类型" prop="targetId">
						<el-select v-model="form.targetId" placeholder="请选择" class="width260 margin-right10">
							<el-option v-for="(item,index) in studyGuideAll" :key="index" :value="item.id" :label="item.name">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="发布地区">
						<el-cascader :props="addProps" v-model="form.codeShow" :options="areaData" placeholder="请选择地区"
							class="width260 margin-right10" @change="addAreaChange"></el-cascader>
					</el-form-item>
					<el-form-item label="发布年份">
						<el-date-picker v-model="form.year" type="year" placeholder="请选择发布年份" value-format="yyyy">
						</el-date-picker>
					</el-form-item>
				</div>
				<el-form-item label="公告分类" prop="categoryId">
					<div class="row-me row-center">
						<el-select v-model="form.categoryId" class="width100Per" placeholder="请选择分类">
							<el-option v-for="item in categorysData" :key="item.id" :label="item.name" :value="item.id">
							</el-option>
						</el-select>
						<el-button type="text" @click="clickCreateType" class="margin-left20">新建分类</el-button>
					</div>
				</el-form-item>
				<el-form-item label="简章内容" prop="content">
					<div class="row-me">
						<vue-ueditor-wrap v-model="form.content" editor-id="editor" :config="editorConfig"
							:editorDependencies="['ueditor.config.js','ueditor.all.js']" style="width: 80%;" />
					</div>
				</el-form-item>
				<el-form-item label="是否显示摘要">
					<div class="row-me">
						<el-switch v-model="form.isSummary" class="line-height40"></el-switch>
						<div class="row-me flex-wrap" v-if="form.isSummary">
							<el-form-item label="报名时间">
								<el-date-picker v-model="form.applyDate" type="datetimerange" range-separator="至"
									start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm"
									@change="onApplyDateChange" class="margin-bottom10" format="yyyy-MM-dd HH:mm" @blur="$forceUpdate()">
								</el-date-picker>
							</el-form-item>
							<el-form-item label="考试时间">
								<el-date-picker v-model="form.examDate" type="datetime" placeholder="请选择考试时间"
									value-format="yyyy-MM-dd HH:mm:ss" style="width: 260px;" class="margin-bottom10">
								</el-date-picker>
							</el-form-item>
							<el-form-item label="招收人数">
								<el-input class="width260 margin-bottom10" v-model="form.recruitNum" placeholder="请输入招收人数"></el-input>
							</el-form-item>
							<el-form-item label="考试内容">
								<el-input class="width260 margin-bottom10" v-model="form.examContent" placeholder="请输入考试内容"></el-input>
							</el-form-item>
						</div>
						<el-form-item label="固定标签" v-else>
							<el-input v-model="form.summary" placeholder="请输入固定标签" class="width260"></el-input>
						</el-form-item>
					</div>
				</el-form-item>
				<div class="row-me flex-wrap">
					<el-form-item label="虚拟浏览量">
						<el-input placeholder="请输入虚拟浏览量" v-model="form.virtualReadNum" class="width260"></el-input>
					</el-form-item>
					<el-form-item label="自增设置">
						<div class="row-me">
							<el-switch v-model="form.addStatus" class="line-height40 margin-right20"></el-switch>
							<div class="row-me" v-if="form.addStatus">
								<el-input class="width120" placeholder="请输入" v-model="form.addMin"></el-input>
								<span class="margin-left5 margin-right5">~</span>
								<el-input class="width120" placeholder="请输入" v-model="form.addMax"></el-input>
								<el-select placeholder="每30分钟/每小时/每天" class="margin-left30 width200" v-model="form.addTime">
									<el-option label="每30分钟" :value="1"></el-option>
									<el-option label="每小时" :value="2"></el-option>
									<el-option label="每天" :value="3"></el-option>
								</el-select>
							</div>
						</div>
					</el-form-item>
				</div>
				<el-form-item label="添加附件">
					<singleUploadLocal :isAnnouncement="true" :value="fileList" isFile
						message="支持扩展名: .doc .xls .pdf .rar .zip格式上传,大小不超过200M" @input="getFile">
					</singleUploadLocal>
				</el-form-item>
				<el-form-item label="上传分享图标">
					<el-image v-if="form.image" class="width100 height100 radius6 margin-right10" :src="form.image"
						:preview-src-list="[form.image]"></el-image>
					<el-button type="text" @click="clickImg(2)">点击图片库</el-button>
				</el-form-item>
				<div class="row-me flex-warp">
					<el-form-item label="选择商品">
						<el-select filterable clearable v-model="form.productIds" placeholder="请选择商品" class="width300">
							<el-option v-for="(item,index) in productData" :key="index" :label="item.name"
								:value="item.id+''"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="选择商品">
						<el-select filterable clearable v-model="form.productIds2" placeholder="请选择商品" class="width300">
							<el-option v-for="(item,index) in productData" :key="index" :label="item.name"
								:value="item.id+''"></el-option>
						</el-select>
					</el-form-item>
				</div>
				<el-form-item label="发布人">
					<el-input placeholder="请输入发布人" v-model="form.publishBy" class="width260"></el-input>
				</el-form-item>
				<el-form-item label="发布时间">
					<el-date-picker v-model="form.publishDate" type="date" placeholder="请选择发布时间"
						value-format="yyyy-MM-dd HH:mm:ss" style="width: 260px;">
					</el-date-picker>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="visible = false">取 消</el-button>
				<el-button @click="handleSave" type="primary" v-if="!isDetail">确 认</el-button>
			</span>
		</el-dialog>

		<!-- 新增编辑分类 -->
		<el-dialog title="分类" :visible.sync="typeVisible" width="40%" @close="getCategotyData">
			<el-table :data="typeTableData" border>
				<el-table-column label="分类名称" align="center">
					<template slot-scope="{row}">
						<el-input v-model="row.name" v-if="row.isEdit"></el-input>
						<span v-else>{{row.name}}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" width="200">
					<template slot-scope="scope">
						<el-button type="text" v-if="scope.row.isEdit" @click="handleTypeAdd(scope.row)">保存</el-button>
						<el-button type="text" @click="$set(scope.row,'isEdit',true)" v-else>编辑</el-button>
						<el-popconfirm title="确认删除？" @confirm="handleTypeDel(scope.row.id)">
							<el-button slot="reference" type="text">删除</el-button>
						</el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
			<div class="margin-top10">
				<el-pagination background layout="->,total,sizes, prev, pager, next" :current-page="typeQuery.pageNumber"
					:page-sizes="[10, 20, 30, 40]" @size-change="handleSizeTypeChange" :page-size="typeQuery.pageSize"
					:total="pageTypeTotal" @current-change="handlePageTypeChange"></el-pagination>
			</div>
			<div class="margin-top20 row-me center-all">
				<el-input v-model="typeQuery.name" placeholder="请输入分类名称" class="margin-right20 width300"></el-input>
				<el-button type="primary" @click="handleTypeAdd">新增</el-button>
			</div>
			<span slot="footer">
				<el-button type="primary" @click="typeVisible = false">关 闭</el-button>
			</span>
		</el-dialog>

		<imgLibrary ref="imgLibrary" @handleImgOk="handleImgOk"></imgLibrary>

	</div>
</template>

<script>
	import imgLibrary from "@/components/imgLibrary"
	import singleUploadLocal from "@/components/Upload/singleUploadLocal"
	export default {
		components: {
			imgLibrary,
			singleUploadLocal
		},
		data() {
			let that = this
			return {
				query: {
					pageNumber: 1,
					pageSize: 30
				},
				tableData: [],
				pageTotal: 0,
				props: {
					emitPath: false,
					value: 'id',
					label: 'name',
					checkStrictly: true,
				},
				addProps: {
					value: 'id',
					label: 'name',
					checkStrictly: true,
					multiple: true
				},
				areaData: [],
				organAll: [],
				miniData: [],
				dateTime: [],
				title: '',
				visible: false,
				form: {},
				iconFileList: [],
				isDetail: false,
				labelList: [],
				fileList: [],
				studyGuideAll: [],
				rules: {
					title: {
						required: true,
						message: '请输入简章标题',
						trigger: 'blur'
					},
					targetId: {
						required: true,
						message: '请选择发布类型',
						trigger: 'change'
					},
					categoryId: {
						required: true,
						message: '请选择公告分类',
						trigger: 'change'
					},
					content: {
						required: true,
						message: '请输入简章内容',
						trigger: 'blur'
					}
				},
				categorysData: [],
				typeVisible: false,
				typeTableData: [],
				typeQuery: {
					pageNumber: 1,
					pageSize: 10
				},
				pageTypeTotal: 0,
				imgIndex: 0,
				editorConfig: {
					serverUrl: process.env.BASE_API + "/file/upload",
					UEDITOR_HOME_URL: '/static/UEditor/',
					toolbarCallback: function(cmd, editor) {
						switch (cmd) {
							case 'insertimage':
								that.imgIndex = 1
								that.$refs.imgLibrary.show()
								return true;
						}
					}
				},
				productData: []
			}
		},
		mounted() {
			// this.getOrganAll()
			this.getAll()
			this.getTagList()
			this.getData()
			this.getStudyGuideAll()
			this.getAllArea()
			this.getCategotyData()
			this.getProduceData()
		},
		methods: {
			//获取商品下拉
			async getProduceData() {
				const res = await this.$get('/admin/mall/pageProduct', {
					pageNumber: 1,
					pageSize: 9999
				})
				if (res.code == 1000) {
					this.productData = res.data.list
				}
			},
			onApplyDateChange(val) {
				this.form.applyStartDate = ''
				this.form.applyEndDate = ''
				if (val && val.length != 0) {
					this.form.applyStartDate = val[0]
					this.form.applyEndDate = val[1]
				}
			},
			//获取公告分类
			async getCategotyData() {
				const res = await this.$get('/admin/articleNotice/getCategory')
				if (res.code == 1000) {
					this.categorysData = res.data
				}
			},
			//分类分页
			handlePageTypeChange(val) {
				this.typeQuery.pageNumber = val
				this.getTypeData();
			},
			handleSizeTypeChange(val) {
				this.typeQuery.pageSize = val
				this.getTypeData();
			},
			//点击新建分类
			clickCreateType() {
				this.getTypeData()
				this.typeVisible = true
			},
			// 新增编辑分类
			handleTypeAdd(row) {
				let params = {
					name: this.typeQuery.name
				}
				if (row.id) {
					params = {
						id: row.id,
						name: row.name
					}
				}
				this.$post('/admin/articleNotice/saveArticleCategory', params).then(res => {
					if (res.code == 1000) {
						this.$message.success('编辑成功')
						this.getTypeData()
						row.isEdit = false
					}
				})
				this.typeQuery.name = ''
			},
			//删除分类
			async handleTypeDel(id) {
				const res = await this.$get('/admin/articleNotice/deleteCategory', {
					id
				})
				if (res.code == 1000) {
					this.$message.success('删除成功')
					this.getTypeData()
				}
			},
			//获取分类数据
			async getTypeData() {
				const res = await this.$get('/admin/articleNotice/pageCategory', this.typeQuery)
				if (res.code == 1000) {
					this.typeTableData = res.data.list
					this.pageTypeTotal = res.data.totalRow
				}
			},
			addAreaChange(val) {
				this.form.codeShow = []
				this.form.province = []
				this.form.city = []
				if (val.length != 0) {
					this.form.codeShow = val
					for (let item of val) {
						if (item[0] || item[0] === 0) {
							if (item[0] === 0) {
								this.form.province.push(item[0])
								this.form.city.push(item[0])
							} else {
								this.form.province.push(item[0])
							}
						}
						if (item[1]) {
							this.form.city.push(item[1])
						}
					}
				}
				this.form.province = [...new Set(this.form.province)].toString()
				this.form.city = [...new Set(this.form.city)].toString()
			},
			//点击图片库
			clickImg(index) {
				this.imgIndex = index
				this.$refs.imgLibrary.show()
			},
			//获取选中的图片
			handleImgOk(data) {
				if (this.imgIndex == 1) {
					let val = this.form.content + `<img src='${data}'>`
					this.$set(this.form, 'content', val)
				} else {
					this.$set(this.form, 'image', data)
				}
			},
			//获取省市区树
			async getAllArea() {
				const res = await this.$get('/admin/area/getAllArea')
				if (res.code == 1000) {
					this.areaData = this.$.getTreeData(res.data)
					this.areaData.unshift({
						adcode: '0',
						name: '默认',
						parentId: 0,
						id: '0'
					})
				}
			},
			//获取发布类型
			async getStudyGuideAll() {
				const res = await this.$get('/admin/user/studyGuideAll', {
					pageNumber: 1,
					pageSize: 9999,
					level: 1
				})
				if (res.code == 1000) {
					this.studyGuideAll = res.data.list
				}
			},
			//获取附件
			getFile(data) {
				this.form.attach = []
				if (data && data.length != 0) {
					for (var i = 0; i < data.length; i++) {
						this.form.attach.push({
							title: data[i].name,
							value: data[i].url
						})
					}
				}
			},
			//获取标签下拉
			async getTagList() {
				const res = await this.$post('/admin/label/labelList')
				if (res.code == 1000) {
					this.labelList = res.data.list
				}
			},
			handleAdd(row, type) {
				this.isDetail = false
				if (row.id) {
					this.title = '编辑'
					if (type == 1) {
						this.isDetail = true
						this.title = '详情'
					}
					this.form = JSON.parse(JSON.stringify(row))
					this.form.addStatus = this.form.addStatus ? true : false
					this.form.codeShow = this.form.codeShow ? JSON.parse(this.form.codeShow) : []
					if (this.form.productIds) {
						let productIdsArr = this.form.productIds.split(',')
						if (productIdsArr[0]) {
							this.form.productIds = productIdsArr[0]
						}
						if (productIdsArr[1]) {
							this.form.productIds2 = productIdsArr[1]
						}
					}
					if (this.form.applyStartDate && this.form.applyEndDate) {
						this.form.applyDate = [this.form.applyStartDate, this.form.applyEndDate]
					}
					if (this.form.attach) {
						this.form.attach = JSON.parse(this.form.attach)
						this.fileList = []
						for (let item of this.form.attach) {
							this.fileList.push({
								name: item.title,
								url: item.value
							})
						}
					} else {
						this.form.attach = []
						this.fileList = []
					}
				} else {
					this.title = '新建'
					this.form = {
						content: ''
					}
					this.fileList = []
					this.form.content = ''

				}
				this.visible = true
			},
			//删除
			async handleDel(id) {
				const res = await this.$postForm('/admin/articleNotice/articleNoticeDeleted', {
					id
				})
				if (res.code == 1000) {
					this.$message.success('删除成功')
					this.getData()
				}
			},
			handleSave() {
				this.$refs['form'].validate((valid) => {
					if (valid) {
						let productIdsArr = []
						if (this.form.productIds) {
							productIdsArr.push(this.form.productIds)
						}
						if (this.form.productIds2) {
							productIdsArr.push(this.form.productIds2)
						}
						this.form.productIds = productIdsArr.toString()
						this.form.attach = this.form.attach ? JSON.stringify(this.form.attach) : this.form.attach
						this.form.labelName = this.form.labelName ? this.form.labelName.toString() : ''
						this.form.codeShow = this.form.codeShow ? JSON.stringify(this.form.codeShow) : ''
						this.form.addStatus = this.form.addStatus ? 1 : 0
						this.$post('/admin/articleNotice/addArticleNotice', this.form).then(res => {
							if (res.code == 1000) {
								this.$message.success('操作成功')
								this.getData()
								this.visible = false
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			//分页
			handlePageChange(val) {
				this.query.pageNumber = val
				this.getData();
			},
			handleSizeChange(val) {
				this.query.pageSize = val
				this.getData();
			},
			//获取列表数据
			async getData() {
				this.query.roleType = 1
				const res = await this.$get('/admin/articleNotice/pageArticleNotice', this.query)
				if (res.code == 1000) {
					this.tableData = res.data.list
					this.pageTotal = res.data.totalRow
				}
			},
			//搜索
			handleSearch() {
				this.query.pageNumber = 1
				this.getData();
			},
			//重置
			handleRefresh() {
				this.query = {
					pageNumber: 1,
					pageSize: 30
				}
				this.dateTime = []
				this.getData();
			},
			//发布时间变动
			onDateChange(val) {
				if (val.length == 0) {
					this.query.startTime = ''
					this.query.endTime = ''
				} else {
					this.query.startTime = val[0]
					this.query.endTime = val[1]
				}
			},
			//获取所有科目
			async getAll() {
				const res = await this.$get('/admin/minis/getAll')
				if (res.code == 1000) {
					this.miniData = res.data
				}
			},
			// //获取机构下拉
			// async getOrganAll() {
			// 	const res = await this.$postForm('/admin/messages/organAll', {
			// 		pageNumber: 1,
			// 		pageSize: 9999
			// 	})
			// 	if (res.code == 1000) {
			// 		this.organAll = res.data.list
			// 	}
			// },
			//报考地区变动
			areaChange(val, key) {
				this[key].city = ''
				this[key].province = ''
				if (val.length != 0) {
					this[key].city = val[0]
					this[key].province = val[1]
				}
			},
		}
	}
</script>